package org.fxcontainer.utils
{
	import org.as3commons.logging.ILogger;
	import org.as3commons.logging.LoggerFactory;
	import org.as3commons.reflect.MethodInvoker;
	import org.as3commons.reflect.Type;
	import org.fxcontainer.controls.IControl;
	
	/**
	 * Control method invokator. 
	 * @author Izzet_Mustafayev
	 */
	public class ControlInvokeHelper
	{
		/**
		 * The <code>ILogger</code> instance.
		 */
		static private var logger:ILogger = LoggerFactory.getClassLogger(ControlInvokeHelper);
		
		/**
		 * Executes control method.
		 * @param control the control to execute method of
		 * @param methodName the method name to execute
		 * @param args the arguments to pass to method invokation
		 */
		static public function execute(control:IControl, methodName:String, ... args:Array):void 
		{
			logger.debug("Enter control execution phase.");
			if (null != control && null != methodName){
				var type:Type = Type.forInstance(control);
				//check method existence
				if (null == type.getMethod(methodName)){
					throw Error("There is no such method within control:" + control.controlName());
				}
				logger.debug("Invoking method: "+methodName+" of the control: "+control.controlName()+".");
				//invoke method
				var methodInvoker:MethodInvoker = new MethodInvoker();
				methodInvoker.target = control;
				methodInvoker.method = methodName;
				methodInvoker.arguments = args;
				methodInvoker.invoke();
			}
			if (null == control || null == methodName){
				logger.error("Cannot execute control method.");
			}
		}
	}
}